#! user/bin/python
# -*- coding: utf-8 -*-

"""
Created on 2021/8/24 15:23

@File: 使用可复用的表单布局类_getfile-use-form.py
@Author: LT
@Description: Use of this module
"""
from tkinter import Tk, mainloop
from tkinter.messagebox import showinfo
from 可复用的表单布局类_form import Form
import 简单的命令行模式_getfile as getfile
import os


class GetfileForm(Form):
    def __init__(self, oneshot=False):
        root = Tk()
        root.title(os.path.split(__file__)[-1])
        labels = ["Server Name", "Port Number", "File Name", "Target File Dir"]
        Form.__init__(self, labels, root)                                   # 这一步就生成了GUI前端，然后把labels传入给它
        self.oneshot = oneshot

    def onSubmit(self):
        """重写onSubmit，在Form类中它只是简单地打印而已"""
        Form.onSubmit(self)                                                 # 先调用这个类打印一下fields
        # 获取条目值
        target_file_dir = self.content['Target File Dir'].get()
        portnumber = self.content['Port Number'].get()
        servername = self.content['Server Name'].get()
        filename = self.content['File Name'].get()
        if target_file_dir:
            os.chdir(target_file_dir)
        portnumber = int(portnumber)

        # 创建客户端连接
        getfile.client(servername, portnumber, filename)
        showinfo(os.path.split(__file__)[-1], "Download Completed")
        if self.oneshot:                                                    # 如果只用一次的话，就退出；否则会留在目标文件所在目录
            Tk().quit()


if __name__ == '__main__':
    GetfileForm()
    mainloop()
